﻿/****************************************************************************************************************
*                                                                                                               *
* Copyright (C) 2011 5173.com                                                                                   *
* This project may be copied only under the terms of the Apache License 2.0.                                    *
* Please visit the project Home Page http://bqqapicsharp.codeplex.com/ for more detail.                         *
*                                                                                                               *
****************************************************************************************************************/

namespace BQQAPIClient.Core.Protocol
{
    using System.Linq;
    using System.Threading;
    using BQQAPIClient.Core.Business;
    using BQQAPIClient.Core.Utility;

    [NotifyPackage(23, 102)]
    internal class GetAllKefuStatusNotifyPackage : IParseOtherData
    {
        private AbstractNotifyPackage abstractNotifyPackage;
        internal KefuStatusList kefuStatusList;

        public GetAllKefuStatusNotifyPackage(AbstractNotifyPackage package)
        {
            this.abstractNotifyPackage = package;
        }

        public void ParseOtherData()
        {
            kefuStatusList = new KefuStatusList();
            var count = 0;
            var result = abstractNotifyPackage.otherData.Where(tlv => tlv.t == KefuStatusList.TCount).ToList();
            if (result.Count > 0) count = result.First().v.GetLittleEndianUInt16();
            for (int i = 0; i < count; i++)
            {
                kefuStatusList.Add(new KefuStatus());
            }
            result = abstractNotifyPackage.otherData.Where(tlv => tlv.t == KefuStatus.TKefuID).ToList();
            if (result.Count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    kefuStatusList[i].KefuID = result.First().v.GetLittleEndianUInt16(i * sizeof(ushort));
                }
            }
            result = abstractNotifyPackage.otherData.Where(tlv => tlv.t == KefuStatus.TkefuStatusEnum).ToList();
            if (result.Count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    kefuStatusList[i].KefuStatusEnum = (KefuStatusEnum)result.First().v.GetLittleEndianUInt16(i * sizeof(ushort));
                }
            }
            kefuStatusList.ForEach(kefuStatus =>
            {
                Logger.Debug("GetAllKefuStatusNotifyPackage : " + kefuStatus.ToString());
            });
            if (BQQAPI.GetAllKefuStatusAction != null)
            {
                ThreadPool.QueueUserWorkItem(state =>  BQQAPI.GetAllKefuStatusAction(kefuStatusList));
            }
        }
    }
}
